<template>
  <l-user-d-select
    v-loading="pageLoading"
    element-loading-text="数据保存中"
    ref="main"
  />
</template>
<script>
const api = window.$api.organization.userRelation;
const apiUser = window.$api.organization.user;
export default {
  data() {
    return {
      pageLoading: false,
      objectType: 1,
      objectId: undefined,
    };
  },
  computed: {},
  created() {},
  methods: {
    resetForm() {
      this.$refs.main.resetForm();
    },
    async setForm(objectId, objectType) {
      this.objectId = objectId;
      this.objectType = objectType;
      let selectList =
        (await this.$awaitWraper(api.getList(this.objectId))) || [];

      const userList = [];
      selectList.forEach((item) => {
        if (item.f_Type == undefined) {
          userList.push(item.f_UserId);
        }
      });
      if (userList.length > 0) {
        const userRes =
          (await this.$awaitWraper(apiUser.getUsers(String(userList)))) || [];
        const userMap = {};
        userRes.forEach((user) => {
          userMap[user.f_UserId] = user.f_RealName;
        });
        selectList.forEach((item) => {
          if (item.f_Type == undefined) {
            item.f_Type = 1;
            item.f_Name = userMap[item.f_UserId]
          }
        });
      }
      await this.$refs.main.setForm(selectList);
    },

    async saveForm() {
      this.pageLoading = true;
      const list = this.$refs.main.getForm();
      await api.saveRoles(this.objectId, list);
      this.pageLoading = false;
    },
  },
};
</script>
